package com.ht.demo.service.manager.system.impl;

import com.ht.demo.common.appexception.AppErrorCodeEnum;
import com.ht.demo.common.appexception.AppException;
import com.ht.demo.common.page.PageBO;
import com.ht.demo.common.pojo.bo.system.SysPostBO;
import com.ht.demo.common.pojo.qto.system.SysPostQTO;
import com.ht.demo.dao.domain.system.SysPostDO;
import com.ht.demo.service.convert.system.SysPostBeanUtil;
import com.ht.demo.service.manager.system.SysPostManager;
import com.ht.demo.service.mapper.system.SysPostMapper;
import com.ht.demo.service.mapper.system.SysUserPostMapper;
import com.ht.demo.service.system.UserTokenManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author hdengxian
 * @Date 2023/5/26 15:47
 */
@Component
public class SysPostManagerImpl implements SysPostManager {
    @Resource
    private SysPostMapper sysPostMapper;
    @Resource
    private SysUserPostMapper sysUserPostMapper;

    @Override
    public Boolean insertPost(SysPostBO post) throws AppException {
        SysPostBeanUtil.savePostCheckParam(post);
        if (sysPostMapper.countByCondition(post.getPostName()) > 0) {
            throw new AppException("岗位名称已被占用");
        }
        post.setCreateBy(UserTokenManager.getInstance().getLoginUser().getUserName());
        post.setCreateTime(new Date());
        return sysPostMapper.save(SysPostBeanUtil.sysPostBOToDO(post));
    }

    @Override
    public Boolean updatePost(SysPostBO post) throws AppException {
        if (post == null || post.getSysPostId() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_ERROR);
        }
        SysPostDO old = sysPostMapper.getById(post.getSysPostId());
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATE_NOT_EXISTS);
        }
        if (StringUtils.isNotBlank(post.getPostName()) && !post.getPostName().equals(old.getPostName())) {
            if (sysPostMapper.countByCondition(post.getPostName()) > 0) {
                throw new AppException("岗位名称已被占用");
            }
        }
        post.setUpdateBy(UserTokenManager.getInstance().getLoginUser().getUserName());
        post.setUpdateTime(new Date());
        return sysPostMapper.updateById(SysPostBeanUtil.sysPostBOToDO(post));
    }

    @Override
    public Boolean deletePost(Long[] sysPostIds) throws AppException {
        if (sysPostIds == null || sysPostIds.length <= 0) {
            return true;
        }
        for (Long sysPostId : sysPostIds) {
            if (sysUserPostMapper.countByPostId(sysPostId) > 0) {
                throw new AppException("岗位已被用户绑定，不能删除");
            }
        }

        return sysPostMapper.removeByIds(Arrays.asList(sysPostIds));
    }

    @Override
    public SysPostBO getPostById(Long sysPostId) throws AppException {
        return SysPostBeanUtil.sysPostDOToBO(sysPostMapper.getById(sysPostId));
    }

    @Override
    public List<SysPostBO> getPostList() throws AppException {
        return SysPostBeanUtil.doList2BOList(sysPostMapper.findSysPostList(new SysPostQTO()));
    }

    @Override
    public PageBO<SysPostBO> getPostForPage(SysPostQTO qto) throws AppException {
        PageBO<SysPostBO> pageBO = new PageBO<>();
        if (qto == null) {
            return pageBO;
        }

        Integer count = sysPostMapper.findForPageCount(qto);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<SysPostBO> list = SysPostBeanUtil.doList2BOList(sysPostMapper.findForPage(qto));
        pageBO.setData(list, count);

        return pageBO;
    }
}
